
package com.widget;

import com.bluesmart.mia.R;
import com.util.DensityUtil;
import com.util.UiUtils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MilkWave extends View  implements SensorEventListener{
	Context mContext;
	
    private static final int WAVE_PAINT_COLOR = Color.WHITE;
    // y = Asin(wx+b)+h
    private static float STRETCH_FACTOR_A = 20;
    private static final int OFFSET_Y = 0;
    private static final int TRANSLATE_X_SPEED_ONE = 5;
    private float mCycleFactorW;

    private int mTotalWidth, mTotalHeight;
    private float[] mYPositions;
    private float[] mResetOneYPositions;
    private int mXOffsetSpeedOne;
    private int mXOneOffset;

    private Paint mWavePaint;
    private DrawFilter mDrawFilter;
    
    int textSize0,textSize1;
    int mOffset = 0;
    
    public MilkWave(Context context, AttributeSet attrs) {
        super(context, attrs);
        mXOffsetSpeedOne = UiUtils.dipToPx(context, TRANSLATE_X_SPEED_ONE);
        mContext = context;
        STRETCH_FACTOR_A = DensityUtil.dip2px(mContext, 5) + 1;

        mWavePaint = new Paint();
        mWavePaint.setAntiAlias(true);
        mWavePaint.setStrokeWidth(DensityUtil.dip2px(mContext, 1) + 1);
        mWavePaint.setStyle(Style.FILL);
        mWavePaint.setColor(WAVE_PAINT_COLOR);
        mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        
        textSize0 = UiUtils.dipToPx(context, 25);
        textSize1 = UiUtils.dipToPx(context, 15);
        mOffset = DensityUtil.dip2px(mContext, 6);
        
        initSensor();
    }

    int disHeight = 0;
    int yPos = 0;
    String mShowText;
    String mShowText0;
    
    public void setProgress(int pos,String text0,String text1)
    {
    	if(pos == 0) yPos = 0;
    	disHeight = getHeight()*pos/360;
    	disHeight = (disHeight/mOffset)*mOffset;
    	
    	
    	mShowText = text0;
    	mShowText0 = text1;
    }
    
    public int getPos()
    {
    	return yPos;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mAngle < mTotalAngle) mAngle += 2;
		else if(mAngle > mTotalAngle) mAngle -= 2;
        
        Log.i("--------------", "mTotalAngle=" + mTotalAngle + "  " + mAngle);
        
        if(yPos > disHeight) yPos -= mOffset;
		else if(yPos < disHeight) yPos += mOffset;
        
        canvas.rotate(mAngle, mTotalWidth/2, mTotalHeight/2);
        canvas.setDrawFilter(mDrawFilter);
        resetPositonY();
        for (int i = 0; i < mTotalWidth; i++) {
            canvas.drawLine(i, mTotalHeight - mResetOneYPositions[i] - yPos - getHeight()/16, i,
                    mTotalHeight,
                    mWavePaint);
        }

        mXOneOffset += mXOffsetSpeedOne;
        if (mXOneOffset >= mTotalWidth) {
            mXOneOffset = 0;
        }
        
        canvas.rotate(-mAngle, mTotalWidth/2, mTotalHeight/2);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(4); 
        paint.setTextSize(textSize0);  
        if(mShowText0.length() > 0)
        {
        	int pos = mTotalHeight - yPos  - mTotalHeight/8;
        	if(pos <= mTotalHeight/3)
        	{
        		pos = mTotalHeight/3;
        		paint.setColor(Color.parseColor("#4762A8"));
        	}
        	
        	int widht = (int)(paint.measureText(mShowText));  
        	
        	canvas.drawText(mShowText, mTotalWidth/2 - widht- 5,pos, paint);

        	paint.setStrokeWidth(2); 
            paint.setTextSize(textSize1); 
            canvas.drawText(mShowText0, mTotalWidth/2, pos, paint);
        }
        else
        {
        	int pos = mTotalHeight - yPos  - mTotalHeight/8;
        	if(pos <= mTotalHeight/3)
        	{
        		pos = mTotalHeight/3;
        		paint.setColor(Color.parseColor("#4762A8"));
        	}

        	int widht = (int)(paint.measureText(mShowText));  
            paint.setTextSize(textSize1); 
        	canvas.drawText(mShowText, mTotalWidth/2 - widht/2,pos, paint);
        }
//        postInvalidate();
    }

    private void resetPositonY() {
        int yOneInterval = mYPositions.length - mXOneOffset;
        System.arraycopy(mYPositions, mXOneOffset, mResetOneYPositions, 0, yOneInterval);
        System.arraycopy(mYPositions, 0, mResetOneYPositions, yOneInterval, mXOneOffset);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
        mYPositions = new float[mTotalWidth];
        mResetOneYPositions = new float[mTotalWidth];
        mCycleFactorW = (float) (2 * Math.PI / mTotalWidth);
        for (int i = 0; i < mTotalWidth; i++) {
            mYPositions[i] = (float) (STRETCH_FACTOR_A * Math.sin(mCycleFactorW * i) + OFFSET_Y);
        }
    }
    
    private class RefreshProgressRunnable implements Runnable {
        public void run() {
            synchronized (MilkWave.this) {
                invalidate();
                postDelayed(this, 5);
            }
        }
    }
    
    int mAngle = 0;
    int mTotalAngle = 0;
    
    private SensorManager sensorManager = null; 
    private Sensor gyroSensor = null; 
    
    void initSensor()
    {
    	post(new RefreshProgressRunnable());
    	
    	sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 
    	gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    	
    	sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_UI); //为传感器注册监听器 
    }
    
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onSensorChanged(SensorEvent arg0) {
		float angle = arg0.values[SensorManager.DATA_X];
		if(Math.abs(mTotalAngle - angle*10) < 5) return;
		mTotalAngle = (int)(angle*10);

		if(Math.abs((mTotalAngle%2)) > 0) mTotalAngle = mTotalAngle + 1;
		
		if(mTotalAngle > 45) mTotalAngle = 46;
		else if(mTotalAngle < -45) mTotalAngle = -46;
	}
}
